﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PSSSystemSound
{
  class Program
  {
    static string startVoice = null;
    static string goVoice = null;

    static System.Media.SoundPlayer startPlayer = null;
    static System.Media.SoundPlayer goPlayer = null;

    static void Main(string[] args)
    {
      if (args.Length > 1)
      {
        startVoice = args[0];
        goVoice = args[1];
      }
      else if (args.Length == 1)
      {
        goVoice = args[0];
      }
      else
        return;

      double startLength = -1;
      if (!System.IO.File.Exists(startVoice))
        startVoice = null;
      else
      {
        startLength = GetLength(startVoice);
        startPlayer = new System.Media.SoundPlayer(startVoice);
        startPlayer.Load();
      }
      if (!System.IO.File.Exists(goVoice))
        goVoice = null;
      else
      {
        goPlayer = new System.Media.SoundPlayer(goVoice);
        goPlayer.Load();
      }


      if (startVoice != null)
        PlayOnce(startVoice);

      if (goVoice != null)
      {
        PlayLoop(goVoice);

        while (true) 
        {
          System.Windows.Forms.Application.DoEvents();
        }

      }
    }

    private static void PlayLoop(string voice)
    {
      goPlayer.PlayLooping();
    }

    private static void PlayOnce(string voice)
    {
      startPlayer.PlaySync();
    }

    private static double GetLength(string fileName)
    {
      double ret;

      try
      {
        Analyse(fileName, out ret);
      }
      catch
      {
        ret = -1;
      }

      return ret;
    }

    private static void Analyse(string fileName, out double length)
    {
      int numOfChannels;
      int sampleRate;
      int bitsPerSample;

      Analyse(fileName, out numOfChannels, out sampleRate, out bitsPerSample, out length);
    }
    private static void Analyse(string fileName, out int numberOfChannels, out int sampleRate, out int bitsPerSample, out double length)
    {
      byte[] data = new byte[36];
      long fileLength;

      System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
      fileLength = fs.Length;
      fs.Read(data, 0, data.Length);
      fs.Close();

      int audioFormat = GetInt2(data, 20);
      if (audioFormat != 1)
        throw new ArgumentException("File is compressed (audioformat: " + audioFormat.ToString() + ").");

      numberOfChannels = GetInt2(data, 22);
      sampleRate = GetInt4(data, 24);
      bitsPerSample = GetInt2(data, 34);

      //double lengthSeconds = ((reader.Channels * reader.BitsPerSample) / 8);
      //lengthSeconds = reader.Length / (reader.SamplingFrequency * lengthSeconds);

      length = (numberOfChannels * bitsPerSample) / 8;
      length = fileLength / (sampleRate * length);
      length = length * 1000; // to miliseconds

    }

    private static int GetInt2(byte[] data, int startIndex)
    {
      int ret = GetInt(data, startIndex, 2);
      return ret;
    }
    private static int GetInt4(byte[] data, int startIndex)
    {
      int ret = GetInt(data, startIndex, 4);
      return ret;
    }
    private static int GetInt(byte[] data, int startIndex, int length)
    {
      int ibase = 255;
      int ret = 0;
      for (int i = 0; i < length; i++)
      {
        int lBase = (int)(Math.Pow(ibase, i));
        ret += data[startIndex + i] * lBase;
      }

      return ret;
    }
  }
}
